package com.zfc.life.controller.backend;

import com.github.pagehelper.PageInfo;
import com.zfc.life.comm.AjaxResult;
import com.zfc.life.comm.BaseUserInfo;
import com.zfc.life.domain.Resource;
import com.zfc.life.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author zfc
 * @Date 2024/6/3 11:23
 * @Description Subject
 */
@Controller
@RequestMapping("/backend/subject")
public class SubjectController {
    String prefix = "backend/";

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/listSubject")
    @ResponseBody
    public AjaxResult listSubject(Integer pageNum, Integer pageSize) {
        if (StringUtils.isEmpty(pageNum) || pageNum <= 1) {
            pageNum = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize <= 10 || pageSize >= 100) {
            pageSize = 10;
        }
        PageInfo<Resource> listResource = resourceService.listResource(pageNum, pageSize);
        return AjaxResult.success(listResource);
    }


    @PostMapping("/addSubject")
    @ResponseBody
    public AjaxResult addResource(@RequestBody Resource resource) {
        resource.setCreateBy(BaseUserInfo.get("name"));
        resource.setCreateTime(LocalDateTime.now());
        resourceService.addResource(resource);
        return AjaxResult.success();
    }

    @PutMapping("/modifySubject")
    @ResponseBody
    public AjaxResult modifySubject(@RequestBody Resource resource) {
        resource.setUpdateBy(BaseUserInfo.get("name"));
        resource.setUpdateTime(LocalDateTime.now());
        return AjaxResult.success(resourceService.modifySubject(resource));
    }

    @DeleteMapping("/deleteSubject/{id}")
    @ResponseBody
    public AjaxResult deleteSubject(@PathVariable Long id) {
        resourceService.deleteSubject(id);
        return AjaxResult.success();
    }

    @PostMapping("/addSubjectDetail")
    @ResponseBody
    public AjaxResult addSubjectDetail(@RequestBody Resource resource) {
        resource.setCreateBy(BaseUserInfo.get("name"));
        resource.setCreateTime(LocalDateTime.now());
        resourceService.addSubjectDetail(resource);
        return AjaxResult.success();
    }
}
